<?php
require '../include/library.inc.php';
require '../include/admin_login.inc.php';

# Ако запазваме
if ($_POST['is_submitted']) {
    $error_message = '';

    require "../include/allowed_types.inc.php";
	
    if (!$_FILES['file']['name']) $error_message .= 'No file is selected!<br/>';
    if(!array_search($_FILES['file']['type'], $allowed_file_types) && $_FILES['file']['name']) $error_message .= 'File format is not allowed!<br/>';

	if(!$error_message) {
		
		$upload_path = "../uploads/files/";
		
		if (is_uploaded_file($_FILES['file']['tmp_name'])) {
				
			$tmp_file = $_FILES['file']['tmp_name'];
			$exp = explode('.',$_FILES['file']['name']);
			$img_type = strtolower(end($exp));
			
			$new_file_name = str_replace(' ', '_', $_FILES['file']['name']);
			
			if(copy($tmp_file, $upload_path.$new_file_name))
			{
				
				$_SESSION['message'] .= 'File '.$new_file_name.' is uploaded!';
				
			} else {
				
				$_SESSION['message'] .= 'Upload error!';
				
			}
			
		}
		
		redirect("files.php");
	}
	
}
	
head_admin("File Manager");
?>
		
			<?php if ($error_message) { ?>
			<div class="message red">
				<h4>Error!</h4>
				<?=$error_message?>
			</div>
			<?php } ?>
			
			<?php if ($_SESSION['message']) { ?>
			<div class="message green">
				<?=$_SESSION['message']?>
			</div>
			<?php unset($_SESSION['message']); } ?>
      
            <div class="topbuttons">
                
            </div>
            
            <h3>File Manager</h3>
			
			<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_submitted" value="1" />
            
            <dl class="clearfix">
            	                	
                Uploaded files can be linked in the text editor. Allowed file formats: zip, rar, pdf, doc, docx, xls, xlsx, ppt, pptx
                            	
            </dl>
            
			<dl class="clearfix">
            
            <dt><label for="file" class="required">New file</label></dt>
            <dd class="inline clearfix">
			<input class="inputbox" name="file" type="file" />
			<input type="submit" name="submit" id="submit" value="Upload">
			</dd>
                        
            </dl>
			</form>
            
            <table id="listing" width="100%" cellpadding="0" cellspacing="0">
			<thead>
				<tr>
					<td>File</td>
					<td>Options</td>
				</tr>
			</thead>
			<tbody>
		<?php
			
			# Първоначлно зареждане
			$directory = "../uploads/files";
			if (is_dir($directory)) {
				$direc = opendir($directory);
			
				while ($file = readdir($direc)) {
					 if (is_file("$directory/$file")) {							
        ?>
            <tr>
            
              <td>
              	<a href="<?=$directory.'/'.$file?>" target="_blank"><?=$file?></a>
              </td>
              
              <td class="icons">
              <a onClick="if (window.confirm('Do you really want to delete this file?\n<?=$file?>')) { document.location='file_del.php?file=<?=$file?>'; return false; }" class="delete" title="Delete">Delete</a>
              </td>
              
              
            </tr>
         <?php 
					}
		 		} 
			
				closedir($direc);
			}	
		 ?>				
			</tbody>
		</table>
            
<?php
	foot_admin(); 
?>